package com.evaluation.kexingcp.psycho.evaluation.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.evaluation.kexingcp.admin.service.SysUserService;
import com.evaluation.kexingcp.common.core.util.R;
import com.evaluation.kexingcp.common.excel.annotation.ResponseExcel;
import com.evaluation.kexingcp.common.log.annotation.SysLog;
import com.evaluation.kexingcp.psycho.evaluation.sys.entity.EvaluationWarningRecordsEntity;
import com.evaluation.kexingcp.psycho.evaluation.sys.service.EvaluationWarningRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 预警记录
 *
 * @author pig
 * @date 2024-07-12 09:48:22
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/evaluationWarningRecords" )
@Tag(description = "evaluationWarningRecords" , name = "预警记录管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EvaluationWarningRecordsController {

    private final EvaluationWarningRecordsService evaluationWarningRecordsService;
    private final SysUserService sysUserService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param evaluationWarningRecords 预警记录
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('pigx_evaluationWarningRecords_view')" )
    public R getEvaluationWarningRecordsPage(@ParameterObject Page page, @ParameterObject EvaluationWarningRecordsEntity evaluationWarningRecords) {
        LambdaQueryWrapper<EvaluationWarningRecordsEntity> wrapper = Wrappers.lambdaQuery();

        wrapper.eq(!Objects.isNull(evaluationWarningRecords.getScaleId()), EvaluationWarningRecordsEntity::getScaleId,evaluationWarningRecords.getScaleId());
        wrapper.eq(!Objects.isNull(evaluationWarningRecords.getTaskId()), EvaluationWarningRecordsEntity::getTaskId,evaluationWarningRecords.getTaskId());
        wrapper.eq(!Objects.isNull(evaluationWarningRecords.getUserId()), EvaluationWarningRecordsEntity::getUserId,evaluationWarningRecords.getUserId());
        wrapper.like(!Objects.isNull(evaluationWarningRecords.getUsername()), EvaluationWarningRecordsEntity::getUserInfo,evaluationWarningRecords.getUsername());
        return R.ok(evaluationWarningRecordsService.page(page, wrapper));
    }

    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/list")
    public R getEvaluationWarningRecordsPage(@ParameterObject EvaluationWarningRecordsEntity evaluationWarningRecords) {
        return R.ok(evaluationWarningRecordsService.getVisiterWarningList(evaluationWarningRecords));
    }


    /**
     * 通过id查询预警记录
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('pigx_evaluationWarningRecords_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(evaluationWarningRecordsService.getById(id));
    }

    /**
     * 新增预警记录
     * @param evaluationWarningRecords 预警记录
     * @return R
     */
    @Operation(summary = "新增预警记录" , description = "新增预警记录" )
    @SysLog("新增预警记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationWarningRecords_add')" )
    public R save(@RequestBody EvaluationWarningRecordsEntity evaluationWarningRecords) {
        return R.ok(evaluationWarningRecordsService.save(evaluationWarningRecords));
    }

    /**
     * 修改预警记录
     * @param evaluationWarningRecords 预警记录
     * @return R
     */
    @Operation(summary = "修改预警记录" , description = "修改预警记录" )
    @SysLog("修改预警记录" )
    @PutMapping
    public R updateById(@RequestBody EvaluationWarningRecordsEntity evaluationWarningRecords) {
        return R.ok(evaluationWarningRecordsService.updateById(evaluationWarningRecords));
    }

    /**
     * 通过id删除预警记录
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除预警记录" , description = "通过id删除预警记录" )
    @SysLog("通过id删除预警记录" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('pigx_evaluationWarningRecords_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(evaluationWarningRecordsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param evaluationWarningRecords 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pigx_evaluationWarningRecords_export')" )
    public List<EvaluationWarningRecordsEntity> export(EvaluationWarningRecordsEntity evaluationWarningRecords,Long[] ids) {
        return evaluationWarningRecordsService.list(Wrappers.lambdaQuery(evaluationWarningRecords).in(ArrayUtil.isNotEmpty(ids), EvaluationWarningRecordsEntity::getId, ids));
    }
}